package per.zqx.routing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import per.zqx.utils.RabbitUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Customer01
 *
 * @author Sa  2021-06-02 18:13
 */
public class Customer01 {
    // 消费者绑定的队列
    private static final String QUEUE_NAME_1 = "queue_name_1";
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitUtils.getConnection();
        Channel channel = connection.createChannel();
        // 声明队列
        channel.queueDeclare(QUEUE_NAME_1, true, false, false, null);
        // 队列通过路由键绑定交换机
        channel.queueBind(QUEUE_NAME_1, Producer.EXCHANGE_NAME, Producer.ROUTER_KEY_1);
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            byte[] body = message.getBody();
            String msg = new String(body, "utf-8");
            System.out.println("Customer1 received a message -> " + msg);
        };
        boolean autoAck = true;
        channel.basicConsume(QUEUE_NAME_1, autoAck, deliverCallback, consumerTag -> {
        });
    }
}
